/**
 * 
 */
package com.alstudio.ui.module.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.main.BasicUsersListActivity;
import com.alstudio.utils.notification.SystemNotificationEnvConfig;
import com.alstudio.utils.notification.SystemSettings;
import com.alstudio.utils.notification.SystemSettings.ModifySettingType;
import com.alstudio.view.tableview.ALBasicListItem;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.ViewItem;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class PrivacySettingActivity extends TitleBarActivity implements
		ALTableViewClickListener {

	private ALTableView tableView;

	// 上线隐身开关
	private ALBasicListItem hideItem;
	// 拒绝陌生人来电
	private ALBasicListItem blockCallItem;
	// 拒绝陌生人留言
	private ALBasicListItem blockMsgItem;
	// 黑名单
	private ALBasicListItem blackListItem;

	// 上线隐身控制
	private final int HIDE_ITEM_INDEX = 0;
	// 拒绝陌生人来电
	private final int BLOCK_CALL_ITEM = 1;
	// 拒绝陌生人留言
	private final int BLOCK_MSG_INDEX = 2;
	// 黑名单
	private final int BLACK_LIST_ITEM_INDEX = 3;

	private boolean hideState;
	private boolean blockCallState;
	private boolean blockMsgState;

	private boolean isDataChanged;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
		initData();
		updateSwitcherState();
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.notice_setting_layout);
		tableView = (ALTableView) findViewById(R.id.aLTableView1);
		hideRightButton(true);
		setCenterTextViewMessage(R.string.TxtPriavcy);
		initTableView();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		hideState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().isHiding();
		blockCallState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().isBlockstrangercall();
		blockMsgState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().isBlockstrangermsg();
	}

	/**
	 * 初始化列表
	 */
	private void initTableView() {

		tableView.clear();

		// 隐身
		hideItem = new ALBasicListItem(getApplicationContext());
		hideItem.setDrawable(R.drawable.icon_system_setting);
		hideItem.title.setText(R.string.TxtOnlineStealth);
		hideItem.setChevronImg(R.drawable.multi_choice_on);
		ViewItem i = new ViewItem(hideItem);
		tableView.addViewItem(i);

		// 隐身说明
		TextView info = new TextView(getApplicationContext());
		info.setText(R.string.TxtAboutOnlineStealth);
		info.setGravity(Gravity.CENTER);
		i = new ViewItem(info);
		i.setClickable(false);
		tableView.addViewItem(i);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 拒绝陌生来电
		blockCallItem = new ALBasicListItem(getApplicationContext());
		blockCallItem.setDrawable(R.drawable.icon_system_setting);
		blockCallItem.title.setText(R.string.TxtRefuseUnknowCall);
		blockCallItem.setChevronImg(R.drawable.multi_choice_on);
		i = new ViewItem(blockCallItem);
		tableView.addViewItem(i);

		// 陌生来电说明
		info = new TextView(getApplicationContext());
		info.setText(R.string.TxtAboutRefuseUnknowCall);
		info.setGravity(Gravity.CENTER);
		i = new ViewItem(info);
		i.setNeedBg(false);
		i.setClickable(false);
		tableView.addViewItem(i);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 拒绝陌生留言
		blockMsgItem = new ALBasicListItem(getApplicationContext());
		blockMsgItem.setDrawable(R.drawable.icon_system_setting);
		blockMsgItem.title.setText(R.string.TxtRefuseUnknowMessage);
		blockMsgItem.setChevronImg(R.drawable.multi_choice_on);
		i = new ViewItem(blockMsgItem);
		tableView.addViewItem(i);

		// 陌生留言说明
		info = new TextView(getApplicationContext());
		info.setText(R.string.TxtAboutRefuseUnknowMessage);
		info.setGravity(Gravity.CENTER);
		i = new ViewItem(info);
		i.setClickable(false);
		i.setNeedBg(false);
		tableView.addViewItem(i);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 黑名单
		blackListItem = new ALBasicListItem(getApplicationContext());
		blackListItem.setDrawable(R.drawable.icon_system_setting);
		blackListItem.title.setText(R.string.TxtBlackList);
		i = new ViewItem(blackListItem);
		tableView.addViewItem(i);

		tableView.setClickListener(this);
		tableView.commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick
	 * (int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		isDataChanged = true;
		switch (index) {
		case HIDE_ITEM_INDEX:
			updateHideState();
			break;
		case BLOCK_CALL_ITEM:
			updateBlockCallState();
			break;
		case BLOCK_MSG_INDEX:
			updateBlockMsgState();
			break;
		case BLACK_LIST_ITEM_INDEX:
			// 跳转到黑名单
			Intent i = new Intent(this, BasicUsersListActivity.class);
			i.putExtra("showType", BasicUsersListActivity.SHOW_TYPE_BLACK_LIST);
			startActivity(i);
			return;
		}
		updateSwitcherState();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		// if (isDataChanged)
		{
			// 保存系统设置
			SystemNotificationEnvConfig.getInstance().saveSystemSettings();
		}
	}

	/**
	 * 刷新隐身开关状态
	 */
	private void updateHideState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setHiding(!hideState);
		hideState = !hideState;
		SystemSettings settings = new SystemSettings();
		settings.setHiding(hideState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_HIDING);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 刷新拒绝陌生来电开关状态
	 */
	private void updateBlockCallState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setBlockstrangercall(!blockCallState);
		blockCallState = !blockCallState;
		SystemSettings settings = new SystemSettings();
		settings.setBlockstrangercall(blockCallState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_BLOCK_STRANGER_CALL);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 更新拒绝陌生来电留言状态
	 */
	private void updateBlockMsgState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setBlockstrangermsg(!blockMsgState);
		blockMsgState = !blockMsgState;
		SystemSettings settings = new SystemSettings();
		settings.setBlockstrangercall(blockMsgState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_BLOCK_STRANGER_MESSAGE);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 更新系统设置相关属性的UI
	 */
	private void updateSwitcherState() {
		// 更新隐身开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.isHiding()) {
			hideItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			hideItem.setChevronImg(R.drawable.multi_choice_off);
		}

		// 更新陌生来电开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.isBlockstrangercall()) {
			blockCallItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			blockCallItem.setChevronImg(R.drawable.multi_choice_off);
		}

		// 更新免陌生留言开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.isBlockstrangermsg()) {
			blockMsgItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			blockMsgItem.setChevronImg(R.drawable.multi_choice_off);
		}
	}

}
